/***************************************************
This file takes all the graduates in the all_students file,
which have been flagged, and extracts when they were enrolled 
***************************************************/

data enrollment (keep = pik degcip_2dig opeid  credithours reportterm 
				reportyear year quarter state cte_flag deglevl_code )
    /view=enrollment;
    
    merge INPUTS.pseo_ny_suny_2002_2020_enr_rsch (in=a rename=(year=year_char
														deglevl_code = deglevl)) 
          INPUTS.pseo_ny_suny_2002_2020_enr_qpik (in=b rename=(qpik_ssn = pik)) ;
    by dqb_source_id ; 

	state = '36' ; 
	deglevl_code = put(deglevl,2.) ;
	if length(deglevl_code) = 1 then deglevl_code = '0'||deglevl_code ; 

    degcip_2dig = substr(degcip,1,2) ;

    year = input(year_char,4.)  ; 
	if term = "Fall" then quarter = 4 ;
	if term = "Winter" then quarter = 1 ; 
	if term = "Spring" then quarter = 2 ; 
	if term = "Summer" then quarter = 3 ; 
	
    if year>=2001 & deglevl_code in ('01','02','03','04') ;
    credithours = input(credit_hours,4.) ;
run;

proc sort data=enrollment out=enrollmentsort; 
    by pik opeid ; 
run;
    
proc sort data=OUTPUTS.all_students_suny out=all ; 
    by pik opeid ; 
run; 

data OUTPUTS.enrollment_long_suny; 
    merge enrollmentsort (in=a) all (in=b) ;
    by pik opeid ; 
    
    if b ; 
    if pik ne '' ; 
    if year = . then year=year_grad ; 
    if quarter = . then quarter = quarter_grad ; 
    enroll= a ; 
    qtime = %qtime(year,quarter,location=datastep) ;
run;

proc sort data=OUTPUTS.enrollment_long_suny out=sample_flag ; 
    by pik opeid ; 
run;


/****************************
Making enrollment wide file
*****************************/

data OUTPUTS.enrollment_wide_suny (keep = pik opeid degcip_2dig credithours 
										flag_cte graduate enr61-enr138 state deglevl_code
										qtime_grad)  ;
	set sample_flag ;
	by pik opeid ;
	
	retain enr21-enr138 ;
	array enr{21:138} enr21-enr138;  
	if qtime <= 138 ; 
	if first.opeid then do ; 
        do i = 21 to 138 ; 
            enr{i} = 0 ; 
        end;
    end;
         
    enr{qtime} = enroll ;  
            
    if last.pik then output ; 
run;

proc sort data=OUTPUTS.enrollment_wide_suny ;
	by pik opeid deglevl_code degcip_2dig ; 
run;
